package com.mx.project.controller;

import com.github.pagehelper.PageHelper;
import com.mx.project.common.resp.ApiResult;
import com.mx.project.constants.Constants;
import com.mx.project.pojo.Product;
import com.mx.project.services.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Api(tags = "操作产品")
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "分类的cid (1,2,3,4)"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", defaultValue = "默认第一页")}
    )
    @GetMapping("classify")
    public ApiResult<List<Product>> getProductClassify(@RequestParam("cid") Integer cid,
                                                       @RequestParam(value = "pageNum",required = false) Integer pageNum) {
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 12);
        ApiResult<List<Product>> apiResult = new ApiResult();
        List<Product> list = productService.getProductByCid(cid,pageNum);
        apiResult.setData(list);
        return apiResult;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", defaultValue = "默认第一页")}
    )
    @GetMapping("search")
    public List<Map<String, Object>> getProduct(
            @RequestParam("keyword") String keyword,
            @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        List<Map<String, Object>> maps = productService.SearchProduct(pageNum, 12, keyword);
        return maps;
    }

    @GetMapping("get")
    public ApiResult<Product> get(@RequestParam Integer pid){
        ApiResult apiResult = new ApiResult();
        Product product = productService.getProductByPid(pid);
        apiResult.setData(product);
        return apiResult;
    }


    @ApiOperation("首页推荐")
    @GetMapping("recommend")
    public ApiResult<List<Product>> recommend(){
        ApiResult apiResult = new ApiResult();
        Random random = new Random();
        List<Product> allProduct = productService.getAllProduct();
        List<Product> lists = new ArrayList<>();
        Set<Integer> set = new HashSet();
        random.nextInt(allProduct.size());
        while (set.size() != 12){
            set.add(random.nextInt(allProduct.size() - 1));
        }
        Iterator<Integer> iterator = set.iterator();
        while (iterator.hasNext()) {
            lists.add(allProduct.get(iterator.next()));
        }
        apiResult.setData(lists);
        return apiResult;
    }
}
